#ifndef TCP_SERVER_POLLER_H
#define TCP_SERVER_POLLER_H

#include <vector>
#include <map>

#include "../../base/Timestamp.h"
#include "../../base/noncopyable.h"
#include "EventLoop.h"

class Channel;

class Poller : muduo::noncopyable {
public:
    typedef std::vector<Channel *> ChannelList;

    Poller(EventLoop *loop);

    virtual ~Poller();

    /**
     * 监听事件集合中有无关心事件发生, 将发生的事件填充到acctiveChannels中
     * @param timeoutMs 监听超时时间
     * @param activeChannels 活动事件集合
     * @return 返回监听返回时间
     */
    virtual Timestamp poll(int timeoutMs, ChannelList *activeChannels) = 0;

    /**
     * 更新文件描述符对应事件
     * @param channel 要更新的事件封装
     */
    virtual void updateChannel(Channel *channel) = 0;

    /**
     * 将文件描述符从关心事件集合中移除
     * @param channel
     */
    virtual void removeChannel(Channel *channel) = 0;

    /**
     * 在当前集合中是否存在该事件集合
     * @param channel
     * @return
     */
    virtual bool hasChannel(Channel *channel) const;

    /**
     * 获取默认Poller调用
     */
    static Poller *newDefaultPoller(EventLoop *loop);

    void assertInLoopThread() const {
        ownerLoop_->assertInLoopThread();
    }

protected:
    typedef std::map<int, Channel *> ChannelMap;
    ChannelMap channels_;
private:
    EventLoop *ownerLoop_;
};


#endif //TCP_SERVER_POLLER_H
